<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['gzys:gzys:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['gzys:gzys:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['gzys:gzys:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['gzys:gzys:export']"
        >导出
        </el-button>
      </el-col>
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

  <!--主子表-->
    <el-table
      v-loading="loading"
      :data="gzysList"
      @selection-change="handleSelectionChange"
      style="width: 100%"
      @expand-change="getSubTableData"
    >
      <el-table-column type="expand">
        <template slot-scope="" #default="scope">
          <h4>购置预算明细</h4>
          <el-table :data="scope.row.childData" v-loading="loading">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="填报单位" prop="dcode" />
            <el-table-column label="资产分类" prop="codee" />
            <el-table-column label="资产名称" prop="zcname" />
            <el-table-column label="单位" prop="per" />
            <el-table-column label="数量" prop="num" />
            <el-table-column label="单价" prop="price" />
            <el-table-column label="总价" prop="numprice" />
            <el-table-column label="购置数量" prop="gzsum" />
            <el-table-column label="购置总价" prop="gzprice" />
          </el-table>
        </template>
      </el-table-column>
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="填报日期" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单位名称" align="center" prop="dname" />
      <el-table-column label="预算数量" align="center" prop="sumnum" />
      <el-table-column label="预算总价" align="center" prop="sumprice" />
      <el-table-column label="已购数量" align="center" prop="num" />
      <el-table-column label="已购总价" align="center" prop="summoney" />
      <el-table-column label="流程状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['gzys:gzys:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['gzys:gzys:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  <!--主子表结束-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <!-- 添加购置预算对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>

      <el-form ref="form" :model="form" :rules="rules" label-width="85px">
        <el-form-item label="单位名称" prop="dname">
          <el-input v-model="form.dname" type="textarea" placeholder="请输入单位名称" />
          <!--下拉树形-->
<!--            <treeselect v-model="form.dname" :options="deptOptions" :normalizer="normalizer" placeholder="请选择公司"-->
<!--             :disable-branch-nodes="true" />-->
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="填报日期" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择填报日期">
          </el-date-picker>
        </el-form-item>
        <el-divider content-position="center">购置预算明细信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddAssetGzyusuanZi">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteAssetGzyusuanZi">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="assetGzyusuanZiList" :row-class-name="rowAssetGzyusuanZiIndex" @selection-change="handleAssetGzyusuanZiSelectionChange" ref="assetGzyusuanZi">
          <el-table-column type="selection" width="30" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
<!--          -->

          <el-table-column label="资产分类" prop="codee" width="120">
            <template slot-scope="scope">
              <el-input v-model="scope.row.codee" placeholder="资产分类" />
            </template>
<!--            <SelectTree-->
<!--              v-model="form.codee"-->
<!--              :props="props"-->
<!--              :options="optionData"-->
<!--              :value="valueId"-->
<!--              :clearable="isClearable"-->
<!--              :accordion="isAccordion"-->
<!--              @getValue="getValue($event)"-->
<!--              style="width: 210px"-->
<!--            />-->
          </el-table-column>
<!--          -->
          <el-table-column label="资产名称" prop="zcname" width="120">
            <template slot-scope="scope">
              <el-input v-model="scope.row.zcname" placeholder="请输入资产名称" />
            </template>
          </el-table-column>
          <el-table-column label="单位" prop="per" width="80">
            <template slot-scope="scope">
              <el-input v-model="scope.row.per" placeholder="请输入单位" />
            </template>
          </el-table-column>
          <el-table-column label="数量" prop="num" width="100">
            <template slot-scope="scope">
              <el-input v-model="scope.row.num" placeholder="请输入数量" />
            </template>
          </el-table-column>
          <el-table-column label="单价" prop="price" width="100">
            <template slot-scope="scope">
              <el-input v-model="scope.row.price" placeholder="请输入单价" />
            </template>
          </el-table-column>

        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--修改购置预算对话框-->
    <el-dialog :title="title" :visible.sync="open1" width="630px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="85px">
        <el-form-item label="单位名称" prop="dname">
          <el-input v-model="form.dname" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="填报日期" prop="startTime">
          <el-date-picker clearable
                          v-model="form.startTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          :disabled="true">
          </el-date-picker>
        </el-form-item>
        <el-divider content-position="center">购置预算明细信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddAssetGzyusuanZi">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteAssetGzyusuanZi">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="assetGzyusuanZiList" :row-class-name="rowAssetGzyusuanZiIndex" @selection-change="handleAssetGzyusuanZiSelectionChange" ref="assetGzyusuanZi">
          <el-table-column type="selection" width="30" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="资产分类" prop="codee" width="120">
            <template slot-scope="scope">
              <el-input v-model="scope.row.codee" placeholder="资产分类" />
            </template>
<!--            <SelectTree-->
<!--              v-model="form.codee"-->
<!--              :props="props"-->
<!--              :options="optionData"-->
<!--              :value="valueId"-->
<!--              :clearable="isClearable"-->
<!--              :accordion="isAccordion"-->
<!--              @getValue="getValue($event)"-->
<!--              style="width: 210px"-->
<!--            />-->
          </el-table-column>
          <el-table-column label="资产名称" prop="zcname" width="120">
            <template slot-scope="scope">
              <el-input v-model="scope.row.zcname" placeholder="请输入资产名称" />
            </template>
          </el-table-column>
          <el-table-column label="单位" prop="per" width="80">
            <template slot-scope="scope">
              <el-input v-model="scope.row.per" placeholder="请输入单位" />
            </template>
          </el-table-column>
          <el-table-column label="数量" prop="num" width="100">
            <template slot-scope="scope">
              <el-input v-model="scope.row.num" placeholder="请输入数量" />
            </template>
          </el-table-column>
          <el-table-column label="单价" prop="price" width="100">
            <template slot-scope="scope">
              <el-input v-model="scope.row.price" placeholder="请输入单价" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGzys, getGzys, delGzys, addGzys, updateGzys,listGS} from "@/api/gzys/gzys";
import {listDept} from '@/api/system/dept'
import {getDept} from "@/api/system/dept";
import {listGzysMx} from "@/api/gzys/zi"
import SelectTree from "./select";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Gzys",
  components: {
    SelectTree,
    Treeselect,
  },
  data() {
    return {
      // 部门管理树选项
      deptOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组cbn
      ids: [],
      // 子表选中数据
      checkedAssetGzyusuanZi: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 购置预算表格数据
      gzysList: [],
      //*******************购置预算明细表格数据***************
      ziList:[],
      childData:[],
      // 购置预算明细表格数据
      assetGzyusuanZiList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sid: null,
        title: null,
        startTime: null,
        dname: null,
        sumnum: null,
        sumprice: null,
        num: null,
        summoney: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      //=-==-=-=-=-=----=-=树形下拉框
      isClearable: true, // 可清空（可选）
      isAccordion: true, // 可收起（可选）
      valueId: 1, // 初始ID（可选）
      props: {
        // 配置项（必选）
        value: "id",
        label: "name",
        children: "children"
        // disabled:true
      },
      // 选项列表（必选）
     list: [
        { id: 1, parentId: 0, name: "办公设备", rank: 1 },
        { id: 2, parentId: 0, name: "信息化设备", rank: 1 },
        { id: 3, parentId: 1, name: "打印机", rank: 2 },
        { id: 4, parentId: 1, name: "投影仪", rank: 2 },
        { id: 5, parentId: 1, name: "投影幕", rank: 2 },
        { id: 6, parentId: 1, name: "多功能一体机", rank: 2 },
        { id: 7, parentId: 1, name: "照相机及器材", rank: 2 },
        { id: 8, parentId: 1, name: "电子白板", rank: 2 },
        { id: 9, parentId: 1, name: "输入输出设备", rank: 2 },
        { id: 10, parentId: 3, name: "A3黑白打印机", rank: 3 },
        { id: 11, parentId: 3, name: "A3彩色打印机", rank: 3 },
        { id: 12, parentId: 3, name: "A4黑白打印机", rank: 3 },
        { id: 13, parentId: 3, name: "A4彩色打印机", rank: 3 },
        { id: 14, parentId: 3, name: "票据打印机", rank: 3 },
        { id: 15, parentId: 3, name: "3d打印机", rank: 3 },
        { id: 16, parentId: 9, name: "绘图设备", rank: 3 },
        { id: 17, parentId: 9, name: "光电设备", rank: 3 },
        { id: 18, parentId: 9, name: "LED显示屏", rank: 3 },
        { id: 19, parentId: 2, name: "计算机", rank: 2 },
        { id: 20, parentId: 19, name: "平板电脑", rank: 3 },
        { id: 21, parentId: 19, name: "台式电脑", rank: 3 },
        { id: 22, parentId: 19, name: "笔记本电脑", rank: 3 },
      ],
    };
  },
  computed: {
    /* 转树形数据 */
    optionData() {
      let cloneData = JSON.parse(JSON.stringify(this.list)); // 对源数据深度克隆
      return cloneData.filter(father => {
        // 循环所有项，并添加children属性
        let branchArr = cloneData.filter(child => father.id == child.parentId); // 返回每一项的子级数组
        branchArr.length > 0 ? (father.children = branchArr) : ""; //给父级添加一个children属性，并赋值
        return father.parentId == 0; //返回第一层
      });
    }
  },
  created() {
    this.getList();
  },
  methods: {
    // 取值-------树形下拉框
    // getValue(value) {
    //   this.valueId = value;
    //   console.log(this.valueId);
    // },
    // //查询公司列表
    // getgslist(){
    //   alert(1)
    //   this.loading = true;
    //   listGS(this.queryParams).then(response => {
    //     this.list = response.rows;
    //     this.loading = false;
    //   });
    // },
    //单位/公司-----下拉树
    /** 转换部门管理数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.fcode,
        children: node.children
      };
    },
    /** 查询部门管理下拉树结构 */
    getTreeselect() {
      alert(123)
      listGS().then(response => {
        this.deptOptions = [];
        const data = {id: 0, fcode: '请选择公司', children: []};
        data.children = this.handleTree(response.data, "id", "fcode");
        this.deptOptions.push(data);
      });
    },
    /** 查询购置预算列表 */
    getList() {
      this.loading = true;
      listGzys(this.queryParams).then(response => {
        this.gzysList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //*************************
    /**查询购置明细*/
    getSubTableData(row){
      this.loading = true;
      listGzysMx(row.id).then(response => {
        row.childData = response.rows;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.open1 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sid: null,
        title: null,
        startTime: null,
        dname: null,
        sumnum: null,
        sumprice: null,
        num: null,
        summoney: null,
        status: null
      };
      this.assetGzyusuanZiList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增固定资产购置预算";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGzys(id).then(response => {
        this.form = response.data;
        this.assetGzyusuanZiList = response.data.assetGzyusuanZiList;
        this.open1 = true;
        this.title = "编辑固定资产购置预算";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.assetGzyusuanZiList = this.assetGzyusuanZiList;
          if (this.form.id != null) {
            updateGzys(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open1 = false;
              this.getList();
            });
          } else {
            addGzys(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除购置预算编号为"' + ids + '"的数据项？').then(function() {
        return delGzys(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 购置预算明细序号 */
    rowAssetGzyusuanZiIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 购置预算明细添加按钮操作 */
    handleAddAssetGzyusuanZi() {
      let obj = {};
      obj.dcode = "";
      obj.codee = "";
      obj.zcname = "";
      obj.per = "";
      obj.num = "";
      obj.price = "";
      obj.numprice = "";
      obj.gzsum = "";
      obj.gzprice = "";
      this.assetGzyusuanZiList.push(obj);
    },
    /** 购置预算明细删除按钮操作 */
    handleDeleteAssetGzyusuanZi() {
      if (this.checkedAssetGzyusuanZi.length == 0) {
        this.$modal.msgError("请先选择要删除的购置预算明细数据");
      } else {
        const assetGzyusuanZiList = this.assetGzyusuanZiList;
        const checkedAssetGzyusuanZi = this.checkedAssetGzyusuanZi;
        this.assetGzyusuanZiList = assetGzyusuanZiList.filter(function(item) {
          return checkedAssetGzyusuanZi.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleAssetGzyusuanZiSelectionChange(selection) {
      this.checkedAssetGzyusuanZi = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gzys/gzys/export', {
        ...this.queryParams
      }, `gzys_${new Date().getTime()}.xlsx`)
    },

  }
};

</script>
<style>
h4{
  color:green;
}
</style>
